#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

vector<double> generateSignal(double freq, double duration, double sampleRate)
{
    vector<double> signal;
    int totalSamples = static_cast<int>(duration * sampleRate);
    for (int i = 0; i < totalSamples; ++i)
    {
        double t = i / sampleRate;
        signal.push_back(sin(2 * M_PI * freq * t));
    }
    return signal;
}

vector<double> sampleSignal(const vector<double> &signal, double sampleRate, double targetSampleRate)
{
    vector<double> sampled;
    int step = static_cast<int>(sampleRate / targetSampleRate);
    for (int i = 0; i < signal.size(); i += step)
    {
        sampled.push_back(signal[i]);
    }
    return sampled;
}

vector<double> reconstructSignal(const vector<double> &samples, double targetSampleRate, double originalSampleRate)
{
    vector<double> reconstructed;
    int step = static_cast<int>(originalSampleRate / targetSampleRate);
    for (int i = 0; i < samples.size(); ++i)
    {
        for (int j = 0; j < step; ++j)
        {
            reconstructed.push_back(samples[i]);
        }
    }
    return reconstructed;
}

int main()
{
    double signalFreq = 1000;
    double duration = 0.01;
    double nyquistRate = 2 * signalFreq;

    auto originalSignal = generateSignal(signalFreq, duration, 10000);

    auto sampledNyquist = sampleSignal(originalSignal, 10000, nyquistRate);

    auto sampledLow = sampleSignal(originalSignal, 10000, 1.5 * signalFreq);

    auto reconstructedNyquist = reconstructSignal(sampledNyquist, nyquistRate, 10000);
    auto reconstructedLow = reconstructSignal(sampledLow, 1.5 * signalFreq, 10000);

    cout << "Nyquist 采样重建信号长度: " << reconstructedNyquist.size() << endl;
    cout << "低于 Nyquist 采样重建信号长度: " << reconstructedLow.size() << endl;
    return 0;
}